package tech.aistar.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.aistar.pojo.Cart;
import tech.aistar.pojo.Result;
import tech.aistar.service.CartService;
import tech.aistar.service.UserService;

import java.util.List;

/**
 * 本来用来演示:
 *
 * @author success
 * @date 2025/3/4 16:02:42
 */
@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping
    public Result index(HttpServletRequest request){
        //思考: 如何拿到登录的用户的id
        //TODO...
        //从request作用域中将userId获取出来
        Integer userId = (Integer) request.getAttribute("userId");
        List<Cart> cartList = cartService.listByUser(userId);
        return new Result("200","登录用户的购物车信息!",cartList);
    }

    @DeleteMapping
    public Result delete(Integer cartId,HttpServletRequest request){
        //思考: 如何拿到登录的用户的id
        //TODO...
        //从request作用域中将userId获取出来
        Integer userId = (Integer) request.getAttribute("userId");

        int count = cartService.delCart(cartId,userId);
        if(count!=0)
            return new Result("200","删除购物车成功!");

        return new Result("500","购物车删除失败!");
    }


    @GetMapping("search")
    public Result search(String name,HttpServletRequest request){
        Integer userId = (Integer) request.getAttribute("userId");
        List<Cart> cartList = cartService.findLikeName(name,userId);
        return new Result("200","登录用户的购物车信息!",cartList);
    }


}
